package com.syj.collect.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.syj.collect.app.R;
import com.syj.collect.entry.AllFragmentBean;
import com.syj.collect.entry.CollectBean;
import com.syj.collect.entry.TaskBean;
import com.syj.collect.entry.UserInfoBean;
import com.syj.collect.entry.UserTaskListBean;
import com.syj.collect.http.ApiServise;
import com.syj.collect.http.BaseObserver;
import com.syj.collect.http.BaseRequest;
import com.syj.collect.listnner.ItemListeners;
import com.syj.collect.model.MyTaskDetailsActivity;
import com.syj.collect.model.ProjectDetailActivity;
import com.syj.collect.utils.GlideUtils;
import com.syj.collect.utils.GsonUtil;
import com.syj.collect.utils.SPUtil;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * created by XXX on 2021/3/4.
 */
public class AdapterProjectList extends RecyclerView.Adapter<AdapterProjectList.AdapterProjectHolder> {
    private int type = 0;
    private List<UserTaskListBean.DataDTO.ListDTO> list;
    private int nexttype; //用来区分下一个页面展示的情况
    private Context context;
    private  boolean iscollect ;
    public AdapterProjectList(int i, Context activity) {
        type = i;
        list = new ArrayList();
        context = activity;
    }


    @NonNull
    @Override
    public AdapterProjectHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(0 == type ? R.layout.project_list_item1 : R.layout.project_list_item2, parent, false);
        return new AdapterProjectHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterProjectHolder holder, int position) {

        if (nexttype == 1 || nexttype == 2 || nexttype == 6) {
            holder.collect_state.setVisibility(View.GONE);
            holder.content2.setText(list.get(position).getDescription() + "");
            holder.content3.setText(list.get(position).getStartTime());
        } else {
            holder.collect_state.setVisibility(View.VISIBLE);
            holder.content2.setText(list.get(position).getPollutionStatus() + "");
            holder.content3.setText("");
            holder.collect_state.setImageResource(list.get(position).getIsCollect().equals("1")? R.drawable.collect_true : R.drawable.collect_false);
        }
        if (list.get(position).getUrls()!=null){
            GlideUtils.PicImage(holder.img, ApiServise.BASEURLFILE+list.get(position).getUrls().get(0));
        }else {
            holder.img.setImageResource(R.drawable.zhanwei);
        }


//        holder.img.setImageResource(R.drawable.my_head);
        holder.content1.setText(list.get(position).getName());


        // holder.collect.setText(iscollect?"收藏":"收藏");
        holder.collect_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //holder.collect.setText(iscollect?"取消收藏":"收藏");


//                HashMap hashMap = new HashMap();
//                String value = SPUtil.getPrefString("userInfo", "");
//                Gson gson = new Gson();
//                UserInfoBean.UserInfo gBean = gson.fromJson(value, UserInfoBean.UserInfo.class);
//                hashMap.put("dtId", list.get(position).getId());//数据的id
//
//
//                String str = gson.toJson(hashMap);
//                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
                BaseRequest.getInstance().getApiServise().newAddCollect(list.get(position).getId())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<String>() {
                            @Override
                            public boolean onSuccess(String value) {
                                Log.e("task1结果?", value);
                                CollectBean collectBean = GsonUtil.gsonToBean(value, CollectBean.class);


                                holder.collect_state.setImageResource(collectBean.getData()==1? R.drawable.collect_true : R.drawable.collect_false);
                                return false;
                            }
                            @Override
                            public void onFailure(Throwable e) throws Exception {
                                //view.initFail("网络异常");
                            }
                            @Override
                            public void onEnd() {

                            }
                        });
//                iscollect = !iscollect;
//                holder.collect_state.setImageResource(iscollect ? R.drawable.collect_true : R.drawable.collect_false);

            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    if (nexttype == 1 || nexttype == 2 || nexttype == 6){
                        //type 是 12 6 说明在项目页面点击的是  已采集， 未采集， 新任务 的按钮，需要去任务详情

                        TaskBean.DataBean.ListBean dataBean=new TaskBean.DataBean.ListBean();
                        List<TaskBean.DataBean.ListBean.AddressList> lists=new ArrayList<>();


                        dataBean.setName(list.get(position).getName());
                        dataBean.setStartTime(list.get(position).getStartTime());
                        dataBean.setEndTime(list.get(position).getEndTime());


                        List<UserTaskListBean.DataDTO.ListDTO.AddressList> address= list.get(position).getAddressList();
                        if (address!=null){
                            for (int i = 0; i <address.size() ; i++) {
                                TaskBean.DataBean.ListBean.AddressList addressList=new TaskBean.DataBean.ListBean.AddressList();
                                addressList.setAddress(address.get(i).getAddress());
                                addressList.setLat(address.get(i).getLat());
                                addressList.setLon(address.get(i).getLon());
                                lists.add(addressList);
                            }
                        }
                        dataBean.setAddressList(lists);
                        Intent intent = new Intent(context, MyTaskDetailsActivity.class);
                        intent.putExtra("data", dataBean);
                        context.startActivity(intent);
                    }else {
                        //其他跳转数据页面的详情
                        ProjectDetailActivity.StartAction(holder.itemView.getContext(), list.get(position).getId() + "");
                    }
            }
        });

//        holder.collect_state.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (listener != null) {
//                    listener.onClick(position);
//                }
//            }
//        });

    }

    public void setclickListnner(ItemListeners listener) {
        this.listnner = listener;
    }
    private ItemListeners listnner;

    public void AddData(List<UserTaskListBean.DataDTO.ListDTO> taskListBeanData, int i) {
        this.nexttype = i;
        list.addAll(taskListBeanData);
        notifyDataSetChanged();
    }

    public interface ItemListeners {
        void onmListenner(String id, int pos, boolean iscollect);
    }




    @Override
    public int getItemCount() {
        return list.size();
    }

    public void upData(List<UserTaskListBean.DataDTO.ListDTO> taskListBeanData, int i) {
        this.nexttype = i;
        if (list.size() != 0) {
            list.clear();
        }
        if (taskListBeanData.size() == 0 || taskListBeanData == null) {
            list.clear();
        } else {

            list.addAll(taskListBeanData);
        }
        notifyDataSetChanged();

    }

    public void updataType(int i) {
        type = i;
        notifyDataSetChanged();
    }

    public static class AdapterProjectHolder extends RecyclerView.ViewHolder {

        private final TextView content1;
        private final TextView content2, collect;
        private final TextView content3;
        private final ImageView img;
        private final ImageView collect_state;

        public AdapterProjectHolder(@NonNull View itemView) {
            super(itemView);

            content1 = (TextView) itemView.findViewById(R.id.adapter_pro_item_content1);
            content2 = (TextView) itemView.findViewById(R.id.adapter_pro_item_content2);
            content3 = (TextView) itemView.findViewById(R.id.adapter_pro_item_content3);
            collect = (TextView) itemView.findViewById(R.id.adapter_pro_item_collect);
            collect.setVisibility(View.GONE);
            collect_state = (ImageView) itemView.findViewById(R.id.adapter_pro_item_collect_state);
            img = (ImageView) itemView.findViewById(R.id.adapter_pro_item_img);
        }
    }
}
